/**
 * 扬尘设备实时数据dustDevData.js
 * 版本号： v1.0.0
 */
$(function () {


    //查询所属项目绑定
    bindCombo_project("prId_");

    /**
     *杨尘设备实时数据列表
     */
    var lastChecked = null;
    $("#dustDevData_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/dustDevData/dustDevDataList.do',
        idField: 'prId',
        frozenColumns: [[
            {field: 'prId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'prName', title: '工程项目', width: 180, fixed:true},
                {field: 'pbUnitName', title: '建设单位', width: 150, fixed:true},
                {field: 'poUnitName', title: '施工单位', width: 150, fixed:true},
                {field: 'psUnitName', title: '监理单位', width: 150}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#dustDevData_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#dustDevData_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#dustDevData_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#dustDevData_menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            dustDevData_doSearch();
                        }
                    });
                }
                //设备列表
                if("deviceListBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            deviceList();
                        }
                    });
                }
                //历史数据
                if("historyDataBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            historyData();
                        }
                    });
                }
                //详情按钮
                if("refreshBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            dustDevData_view();
                        }
                    });
                }

            }
        }
    }
    return toolbar;
}


/**
 * 查看项目下相关设备列表
 * @param index
 */
function deviceList(){
    var checked = $("#dustDevData_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#dustDevData_dataList").datagrid("getSelected");
    var prId = row.prId;
    window.location.href = CONTEXT_PATH + "/dustDevData/toDustDeviceList.do?prId="+prId;
}



/**
 * 查看设备历史数据
 * @param index
 */
function historyData(){
    var checked = $("#dustDevData_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#dustDevData_dataList").datagrid("getSelected");
    if(row){
        var prId = row.prId;
        window.location.href = CONTEXT_PATH + "/dustDevRecord/index.do?prId="+prId;
    }
}


/**
 * 执行查询
 */
function dustDevData_doSearch() {
    //刷新dg页码
    refreshPageNumber("dustDevData_dataList");
    $("#dustDevData_dataList").datagrid("reload", {
        prId: $("#prId_").combobox("getValue")
    });
    $("#dustDevData_dataList").datagrid("clearSelections");
    $('#dustDevDataQuery_dialog').dialog('close');
}


/**
 * 查看详情
 */
function dustDevData_view() {

    var checked = $("#dustDevData_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择需要查看的扬尘详情", timeout: 3000, showType: "slide"});
        return;
    }
    var row = $("#dustDevData_dataList").datagrid("getSelected");

    //IFRAM打开窗口
    layoutSub(row.prId , CONTEXT_PATH + "/dustDevData/dustDevDataDesc.do?id=" + row.prId, 'menu_search', true, "扬尘详情");
}

/**
 * 所属项目bind事件
 * @param projectControl
 */
function bindCombo_project(projectControl) {
    $.ajax({
        url: CONTEXT_PATH + "/project/getProjectListForCombo.do",
        type: "get",
        success: function (data) {
            var obj = JSON.parse(data);
            var projectCombo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < obj.length; i++) {
                projectCombo.push({"text": obj[i].prName, "value": obj[i].prId});
            }
            $("#" + projectControl).combobox("loadData", projectCombo);
        }
    })
}




